﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Clients;
using WMS.OutboundManagement.ReturnIitemDetailss;
using WMS.OutboundManagement.ReturnOfGoodss;
using WMS.StorageManagement.BinLocationDefinitions;
using WMS.WarehouseManagement.PurchaseDetailss;
using WMS.WarehouseManagement.PurchaseOrders;

namespace WMS.OutboundManagement.ReturnAcceptances
{
    /// <summary>
    /// 实现退货验收接口
    /// </summary>
    public class ReturnAcceptanceAppService: IApplicationService, IReturnAcceptanceAppService
    {
        /// <summary>
        /// 退货商品详情
        /// </summary>
        private readonly IRepository<ReturnIitemDetails, int> _returnIitemDetailsRepository;


        /// <summary>
        ///  退货商品
        /// </summary>
        private readonly IRepository<ReturnOfGoods, int> _returnOfGoodsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ReturnAcceptanceAppService(IRepository<ReturnIitemDetails, int> returnIitemDetailsRepository, IRepository<ReturnOfGoods, int> returnOfGoodsRepository)
        {
            _returnIitemDetailsRepository = returnIitemDetailsRepository;
            _returnOfGoodsRepository = returnOfGoodsRepository;
        }


        /// <summary>
        /// 退货验收显示
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResult> GetReturnAcceptance(int page, int limit)
        {
            /// <summary>
            /// 进货商品
            /// </summary>
            var returnOfGoodslsList = await _returnOfGoodsRepository.GetListAsync();

            var returnOfGoodsQueryable = returnOfGoodslsList.AsQueryable();

            /// <summary>
            ///  进货商品详情
            /// </summary>
            var returnIitemDetailsList = await _returnIitemDetailsRepository.GetListAsync();

            var returnIitemDetailsQueryable = returnIitemDetailsList.AsQueryable();


            var binLocationDefinition = from a in returnOfGoodsQueryable
                                        join b in returnIitemDetailsQueryable
                                        on a.Id equals b.Pid
                                        select new
                                        {
                                            a.Id,

                                            a.EstimatedTime,
                                            a.OrderNumber,
                                            a.Transportation,
                                            a.Supplier,
                                            a.ReserveArea,
                                            a.Warehouse,
                                            a.OrderType,
                                            b.ProductCode,
                                            b.Age,
                                            b.Sdate,
                                            b.CustomerOrderNumber,
                                            b.OrderName,
                                            b.RegistrationAge,
                                            b.NoRegistration,
                                            b.DateTime,
                                            b.batch,
                                            b.PositionOfStorage,
                                            b.Pallet,
                                            b.Unit,
                                            b.Pid,
                                            b.ListingSdate,
                                        };

            var pagedData = binLocationDefinition.Skip((page - 1) * limit).Take(limit);

            // 获取总数
            int totalCount = binLocationDefinition.Count();

            PagedResult pageResult = new PagedResult
            {
                CurrentPage = page,
                PageCount = (int)Math.Ceiling((double)totalCount / limit),
                RowCount = totalCount,
                Queryable = pagedData
            };

            return await Task.FromResult(pageResult);
        }



        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> UpdateSdate(int id)
        {
            var returnIitemDetails = await _returnIitemDetailsRepository.FirstOrDefaultAsync(x => x.Pid == id);
            if (returnIitemDetails.ListingSdate == 1)
            {
                returnIitemDetails.ListingSdate = 0;
            }
            else
            {
                returnIitemDetails.ListingSdate = 1;
            }
            
            _returnIitemDetailsRepository.UpdateAsync(returnIitemDetails);
            return 1;
        }
    }
}
